package com.ae.shield.common.items.shieldFittings.makers;

import com.ae.shield.common.enchantment.shieldSuffix.SuffixList;
import com.ae.shield.common.items.util.DamageSorter;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.energy.IEnergyStorage;

import javax.annotation.Nonnull;
import java.util.List;

public class AntiMagicShield extends ShieldMakerBase {
    public AntiMagicShield() {
        super(new Properties(), new ShieldProperty()
                .recovery(0.15f)
                .cd(32)
                .maxIntensity(24f)
                .close(1.8f)
                .remote(1.6f)
                .magic(0.4f)
                .other(0f)
                .upperLimit(30f)
                .floorLimit(0f)
                .maxPiles(2)
                .penalty(10f)
                .cost(7));
    }

    @Override
    public void addCtrlInformation(List<ITextComponent> components, ItemStack stack) {
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip1").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip2").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip3").mergeStyle(TextFormatting.GRAY));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip4").mergeStyle(TextFormatting.DARK_PURPLE));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip5").mergeStyle(TextFormatting.BLUE));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip6").mergeStyle(TextFormatting.BLUE));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip7").mergeStyle(TextFormatting.GOLD));
        components.add(new TranslationTextComponent("tooltip.ae.shield.anti_magic_shield.tip8").mergeStyle(TextFormatting.BLUE));
    }

    @Override
    public float onShieldWorked(LivingEntity livingEntity, ItemStack stack, float amount, float residue, DamageSource source, IEnergyStorage energyCap) {
        Entity trueSource = source.getTrueSource();

        if (!(trueSource instanceof LivingEntity)) {
            return residue;
        }

        LivingEntity entity = (LivingEntity) trueSource;
        int level = EnchantmentHelper.getEnchantmentLevel(SuffixList.LEVEL_UP.get(), stack);

        DamageSource damageSource = new DamageSource("shield_maker") {
            @Nonnull
            @Override
            public ITextComponent getDeathMessage(@Nonnull LivingEntity entityLivingBaseIn) {
                String s = "death.attack.anti_magic_shield" + (random.nextInt(3) + 1);
                return new TranslationTextComponent(s, entityLivingBaseIn.getDisplayName(), livingEntity.getDisplayName());
            }
        };
        damageSource.setDamageIsAbsolute();

        if (DamageSorter.sort(source) == DamageSorter.MAGIC) {
            int i = random.nextInt(100);
            if (i < 6) {
                entity.attackEntityFrom(damageSource, entity.getHealth() * (0.1f + level * 0.025f));
            }
            entity.attackEntityFrom(damageSource, entity.getHealth() * (0.05f + level * 0.02f));
        } else if (DamageSorter.sort(source) == DamageSorter.SHIELD) {
            return residue;
        } else {
            int i = random.nextInt(100);
            if (i < 20) {
                entity.attackEntityFrom(damageSource, entity.getHealth() * (0.03f + level * 0.01f));
            }
        }

        return residue;
    }

    @Override
    public void playDefenseVoice(World world, LivingEntity entity) {
        world.playSound(null, entity.getPosX(), entity.getPosY(), entity.getPosZ(), SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.PLAYERS, 5f, 1f);
    }
}
